var myUsername = $('#add_phone');
var myPassword = $('#password');
var myNickname = $('#nickname');
var myCode = $('#code');
var verifyCode = null;

$("#reg").click(function (event) {
    event.preventDefault;
    var getUsername = myUsername.val();
    var getPassword = myPassword.val();
    var getCode = myCode.val();
    var getNickname = myNickname.val();
    if (codeCheck() && myValidateNickname() && myValidatePassword() && myValidateUsername()){

        $.ajax({
            type: 'get',
            url: '/checkNickname',
            dataType: 'json',
            async:false,
            data:{
                nickname:getNickname
            },
            success:function (data) {
                if (data['status'] != 200) {
                    alert("这个名号已经被使用了，为了让仇家能准确地找到你换一个吧 -.-");
                }
            }
        });

        $.ajax({
            type: 'POST',
            url: '/doRegister',
            dataType: 'json',
            async:false,
            data:{
                username: getUsername,
                password: getPassword,
                nickname:getNickname,
                code:getCode,
                verifyCode: verifyCode
            },
            success:function (data) {
                console.log("222")
                if (data['status'] == 0) {
                    alert("服务器发生异常，注册失败")
                } else if (data['status'] == 2) {
                    document.getElementById("someMeg").innerHTML = "用户名已存在";
                } else if (data['status'] == 3) {
                    document.getElementById("someMeg").innerHTML = "验证码错误";
                } else {
                    alert("注册成功");
                    window.location.href("/login")
                }

            },
            error:function error() {
                alert("上传数据错误")
            }
        })
    }else {
        alert("请正确填写信息")
    }
});

function myValidateUsername() {
    var input = document.registerForm.username.value;
    if(input == "" || input == null){
        document.getElementById("someMeg").innerHTML="是电话号码不是QQ号码";
        return false;
    }else{
        document.getElementById("someMeg").innerHTML=" ";
        return true;
    }
}

function myValidatePassword() {
    var input = document.registerForm.password.value;
    if(input == "" || input == null){
        document.getElementById("someMeg").innerHTML="密码基本准则知道不？";
        return false;
    }else{
        document.getElementById("someMeg").innerHTML=" ";
        return true;
    }
}

function myValidateNickname() {
    var input = document.registerForm.nickname.value;
    if(input == "" || input == null){
        document.getElementById("someMeg").innerHTML="来一个响当当的名号吧";
        return false;
    }else{
        document.getElementById("someMeg").innerHTML=" ";
        return true;
    }
}
var AddInterValObj; //timer变量，控制时间
var adcount = 60; //间隔函数，1秒执行
var addCount;//当前剩余秒数

function sendAddmes() {

    var myreg = /^1(3|4|5|7|8|9)\d{9}$/;
    if(!myreg.test($("#add_phone").val()))
    {
        alert('请输入有效的手机号码');
        return false;
    }else{
        addCount = adcount;
        //设置button效果，开始计时
        $("#addSendCode").attr("disabled", "true");
        $("#addSendCode").val(addCount + "秒后重新获取").css({"background-color":"#D1D4D3"});
        AddInterValObj = window.setInterval(SetAddnTime, 1000); //启动计时器，1秒执行一次
        var dealType ="";
        var uid = "";
        var code = "";
        var getUsername = myUsername.val();
        //向后台发送处理数据
        $.ajax({
            type: "POST", //用POST方式传输
            dataType: "JSON", //数据格式:JSON
            url: '/sendMeg', //目标地址
            data: {
                number: getUsername
            },
            error: function () {
                alert("数据请求失败")
            },
            success: function (data){
                verifyCode = data['verifyCode']
                console.log(verifyCode);
            }
        });
    }
}

//timer处理函数
function SetAddnTime() {
    if (addCount == 0) {
        window.clearInterval(AddInterValObj);//停止计时器
        $("#addSendCode").removeAttr("disabled");//启用按钮
        $("#addSendCode").val("重新获取验证码").css({"background-color":"#337ab6"});
    }
    else {
        addCount--;
        $("#addSendCode").val("" + addCount + "秒后重新获取").css({"background-color":"#0a0a0a"});
    }
}

function telphone(){
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
    if(!myreg.test($("#add_phone").val()))
    {
        alert('请输入有效的手机号码');

        return false;
    }
}
$(document).on('blur','.login_ipt',function(){
    telphone();
});

//code 验证
function codeCheck(){
    var input = myCode.val();
    if(input=='' || input == null){
        console.log("验证码" + input);
        document.getElementById("someMeg").innerHTML="验证码不能为空";
        return false;
    }else{
        document.getElementById("someMeg").innerHTML="";
        return true;
    }
}



